#coding=utf-8

require "aio/core"
require "pathname"

class Aio::Module::OutputStyle::Cmds < Aio::Module::OutputStyle

	def initialize
		super({
			:author				=> "Elin",
			:description	=> "这个模块将生成一个文件夹,其中每个文件为各种设备的巡检命令, 此模块不需要Input Module",
			:file_suffix	=> "txt"
		})
	end

	def generate
		# 获得 cmds = {cmd_name => cmd_info}
		cmds = module_manager.get_modules_by_type("cmd")
		
		# 取得pf = {device => [cmd, ...]}
		pf = {}
		cmds.each_pair do |cmd, info|
			device = info[:module_layer_2]
			pf[device] = [] unless pf.has_key?(device)
			pf[device] << info[:klass].cmd_full
		end

		# 各种设备的修改方法
		terminal = {
			"cisco"		=> ["ter leng 0", "ter leng 24"],
			"h3c"			=> ["screen-length 0", "screen-length 24"],
			"maipu"		=> ["more off", "more on"],
			"juniper"	=> [" | no-more"],
		}

		text = {}
		# 生成Terminal格式
		pf.each_pair do |device, arr|
			name = device + "_terminal"
			text[name] = []
			
			# 当是cisco, h3c, maipu 的时候使用一种方式
			if device == "cisco" or device == "h3c" or device == "maipu"
				text[name] << terminal[device][0]
				arr.each do |cmd|
					text[name] << cmd
				end
				text[name] << terminal[device][1]

			# 当是 juniper 的时候使用cmd后面跟参数
			elsif device == "juniper"
				arr.each do |cmd|
					text[name] << cmd + terminal[device][0]
				end
			end

			# 最后将数组改为文本
			text[name] = text[name].join("\r\n!\r\n")
		end

		# 生成空格的格式
		pf.each_pair do |device, arr|
			name = device + "_blank"
			text[name] = arr.join("\r\n#{' '*10}\r\n!\r\n")
			text[name] << "\r\n#{' '*10}\r\n!\r\n"
		end

		pn = Pathname.new(output_file)
		Dir.mkdir(output_file) unless pn.directory?

		text.each_pair do |name, content|
			file = File.new(File.join(output_file, name), "w+")
			file.write(content)
		end
	end
end

